#include "MuxWriter.h"

#define MUX_FRAME_START 0xBF

MuxWriter::MuxWriter(Writer* writer)
{
	_writer = writer;
	_link = 0;
}


MuxWriter::~MuxWriter(void)
{
	_writer = 0;
}


char MuxWriter::Write(char* message, char msg_length)
{
	char* p_msg_end;
	char* p_msg;
	p_msg = message;
	p_msg_end = p_msg + msg_length;

	_writer->Write(MUX_FRAME_START);
	_writer->Write(_link);
	_writer->Write(0x00); //6Bit Flags + 2Bit of Length
	_writer->Write(msg_length);

	while(p_msg < p_msg_end) 
	{
		_writer->Write(*p_msg);
	}

	_writer->Write(_link ^ 0xFF);

	return 0;
}
